package com.tools.cleanmaster.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.tools.ad.util.StatusBarUtil;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.controller.TrashHandler;
import com.tools.cleanmaster.manager.TrashManager;
import com.tools.cleanmaster.model.TrashType;
import com.tools.cleanmaster.model.TrashesData;
import com.tools.cleanmaster.model.item.ImageTrashBucket;
import com.tools.cleanmaster.model.item.TrashItem;
import com.tools.cleanmaster.ui.adapter.ImageAdapter;
import com.tools.cleanmaster.ui.base.SingleActivity;
import com.tools.cleanmaster.ui.card.ui.EmptyLayout;
import com.tools.cleanmaster.ui.view.header.HeadHelper;
import com.tools.cleanmaster.utils.FeatureConfig;
import com.tools.cleanmaster.utils.LogHelper;

import java.util.List;

/**
 * desc: 展示图片分类（如：camera、Screenshots 等）
 * date: 2018/7/4 11:41
 * author: ancun
 */
public class ImageBucketActivity extends SingleActivity {
    private static final String TAG = "ImageBucketActivity";

    private GridView mGridView;
    private ImageAdapter mAdapter;
    private boolean isCreate = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_file);
        StatusBarUtil.setTransparentForImageView(this, findViewById(R.id.titlebar));
        isCreate = true;
        init();
        initData();
    }

    @Override
    protected void doOnResume() {
        if (!isCreate) {
            if (mAdapter != null) {
                List<TrashItem> mBuckets = getTrashItems();
                if (mBuckets == null || mBuckets.isEmpty()) {
                    finish();
                    return;
                }
                mAdapter.setDatas(mBuckets);
            }
        }
        isCreate = false;
    }

    private void init() {
        HeadHelper.setupMainHeader(this, R.id.titlebar, R.string.trash_clean_image_files, this).hideSettingButton();

        EmptyLayout mEmptyView = findViewById(R.id.empty_view);
        mEmptyView.setTips(R.string.trash_clean_empty_summary);
        mGridView = findViewById(R.id.trash_result_view_gv);
        mGridView.setEmptyView(mEmptyView);
    }

    private void initData() {
        List<TrashItem> mBuckets = getTrashItems();
        if (mBuckets == null || mBuckets.isEmpty()) {
            finish();
            return;
        }

        mAdapter = new ImageAdapter(this, mBuckets);
        mGridView.setAdapter(mAdapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int arg2, long arg3) {
                Intent intent = new Intent(ImageBucketActivity.this, ImageShowActivity.class);
                ImageTrashBucket imageTrashBucket = (ImageTrashBucket) adapterView.getItemAtPosition(arg2);
                intent.putExtra(ImageShowActivity.BUCKET_TITLE, imageTrashBucket.bucketName);
                intent.putExtra(ImageShowActivity.BUCKET_ID, imageTrashBucket.id);
                if (FeatureConfig.DEBUG_LOG) {
                    LogHelper.i(TAG, "count=" + imageTrashBucket.count + " size=" + imageTrashBucket.size + " "
                            + "id=" + imageTrashBucket.id);
                }
                startActivity(intent);
            }
        });
    }

    private List<TrashItem> getTrashItems() {
        TrashHandler mTrashHandler = TrashManager.getInstance().getTrashHandler(true);
        if (mTrashHandler == null) {
            return null;
        }

        TrashesData trashesData = mTrashHandler.getTrashesData();
        if (trashesData == null) {
            return null;
        }

        List<TrashItem> mBuckets = trashesData.getTrashesByType(TrashType.IMAGE_FILE);
        if (mBuckets == null || mBuckets.size() == 0) {
            return null;
        }
        return mBuckets;
    }
}
